package com.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.pojo.Department;
import com.pojo.Employee;
import com.service.DepartmentService;
import com.service.EmployeeService;
import com.service.RoleService;
import com.util.LayuiTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@Transactional
@RequestMapping("/emp")
public class EmployeeController {
    @Autowired
    public EmployeeService employeeService;
    @Autowired
    public DepartmentService departmentService;
    @Autowired
    public RoleService roleService;

//    查询员工列表
@RequestMapping("/list")
@ResponseBody
    public String empList(Integer empno, String ename, String role, String dname){
        return JSON.toJSONString(LayuiTable.tableData(employeeService.employeeFindLike(empno, ename, role, dname).size(),
                employeeService.employeeFindLike(empno, ename, role, dname)),
                SerializerFeature.DisableCircularReferenceDetect);
    }



//员工入职
    @RequestMapping("/add")
    @ResponseBody
    public String empadd(String employeelist) throws Exception{
    List<Employee> employees=employeeService.employeeFindAll();
    Integer empno= employees.get(employees.size()-1).getEmpno()+1;
    Map add= JSON.parseObject(employeelist);
    String ename= String.valueOf(add.get("ename"));
    String sex= String.valueOf(add.get("sex"));
    Integer roleid= Integer.parseInt(add.get("job").toString());
    String idcard= String.valueOf(add.get("idcard"));
    String phone= String.valueOf(add.get("phone"));
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date hiredate=simpleDateFormat.parse(add.get("hiredate").toString());
    int mgr= Integer.parseInt(add.get("mgr").toString());
    double sal= Double.parseDouble(add.get("sal").toString());
    double comm= Double.parseDouble(add.get("comm").toString());
    int deptno= Integer.parseInt(add.get("dept").toString());
    Employee employee=new Employee(empno,ename,sex.charAt(0),idcard,phone,hiredate,mgr,BigDecimal.valueOf(sal),BigDecimal.valueOf(comm),new Department(deptno,null,null),null);
    int a=employeeService.insertEmployee(employee);
    int b=roleService.insertCemprole(empno,roleid);
    if (a+b==2){
        return "true";
    }else {
        return "false";
    }
    }
    //全勤：如果当月

//    员工编辑
@RequestMapping("/edit")
@ResponseBody
    public String empedit(String emplist)throws  Exception{
    Map edit= JSON.parseObject(emplist);
    Integer empno= Integer.parseInt(edit.get("empno").toString());
    String ename= String.valueOf(edit.get("ename"));
    String sex= String.valueOf(edit.get("sex"));
    Integer roleid= Integer.parseInt(edit.get("job").toString());
    String idcard= String.valueOf(edit.get("idcard"));
    String phone= String.valueOf(edit.get("phone"));
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date hiredate=simpleDateFormat.parse(edit.get("hiredate").toString());
    int mgr= Integer.parseInt(edit.get("mgr").toString());
    double sal= Double.parseDouble(edit.get("sal").toString());
    double comm= Double.parseDouble(edit.get("comm").toString());
    int deptno= Integer.parseInt(edit.get("dept").toString());
    Employee employee=new Employee(empno,ename,sex.charAt(0),idcard,phone,hiredate,mgr,BigDecimal.valueOf(sal),BigDecimal.valueOf(comm),new Department(deptno,null,null),null);
    int a=employeeService.updateEmployee(employee);
    int b=roleService.updateC_emp_roleByempid(roleid,empno);
    if (a+b==2){
        return "true";
    }else {
        return "false";
    }
    }

//    员工离职
    @RequestMapping("/del")
    @ResponseBody
    public String empdel(Integer empno){
        int b=roleService.deleteC_emp_roleByempid(empno);
        int a=employeeService.deleteEmployee(empno);
        if (a+b==2){
            return "true";
        }else {
            return "false";
        }
    }
}
